package top.saodisheng.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import top.saodisheng.dao.OrderDao;
import top.saodisheng.pojo.Order;
import top.saodisheng.pojo.User;
import top.saodisheng.pojo.Vehicle;
import top.saodisheng.util.Page4Navigator;

import java.util.Date;
import java.util.List;

/**
 * Description:
 *
 * @author 扫地生_saodisheng
 */
@Service
public class OrderService {
    @Autowired
    OrderDao orderDao;

    // 获取数据库中的所有记录
    public List<Order> list() {
        return orderDao.findAll();
    }

    // 获取数据库中的所有记录，按照编号降序
    public Page4Navigator<Order> list(int start, int size, int navigatePage) {
        Page<Order> pageFromJPA = orderDao.findAll(PageRequest.of(start, size,
                Sort.by(Sort.Direction.DESC, "id")));
        return new Page4Navigator<>(pageFromJPA, navigatePage);
    }

    // 通过寄件人获取对应的订单
    public List<Order> listBySender(User sender) {
        return orderDao.findBySender(sender);
    }

    // 通过寄件人、运输工具、订单创建时间查询订单
    public List<Order> listBySenderAndVehicleAndCreateDateBetween(User sender, Vehicle vehicle, Date startTime, Date endTime) {
        return orderDao.findBySenderAndVehicleAndCreateDateBetween(sender, vehicle, startTime, endTime);
    }

    // 通过寄件人、订单创建时间查询订单
    public List<Order> listBySenderAndCreateDateBetween(User sender, Date startTime, Date endTime) {
        return orderDao.findBySenderAndCreateDateBetween(sender, startTime, endTime);
    }

    // 通过运输工具、订单创建时间查询订单
    public List<Order> listByVehicleAndCreateDateBetween(Vehicle vehicle, Date startTime, Date endTime) {
        return orderDao.findByVehicleAndCreateDateBetween(vehicle, startTime, endTime);
    }

    // 通过订单创建时间来查询时间
    public List<Order> listByCreateDateBetween(Date startTime, Date endTime){
        return orderDao.findByCreateDateBetween(startTime, endTime);
    }

    // 添加记录
    public void add(Order bean) {
        orderDao.save(bean);
    }

    // 修改记录
    public void update(Order bean) {
        orderDao.save(bean);
    }
    // 按照编号查询记录
    public Order get(int id) {
        return orderDao.getOne(id);
    }
    // 删除记录
    public void delete(int id) {
        orderDao.deleteById(id);
    }

    // 获取数据库表的记录数
    public int count() {
        return (int)orderDao.count();
    }
}
